const Router = require('koa-router')
const search = new Router()
const db = require('../mysql/index');
const bodyparser = require("koa-body");
search.use(bodyparser())
// var gamelist
//
// db.query(`SELECT game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc" FROM game`,function(result){
//     gamelist = result
// })

search.post('/game', async (ctx)=>{
    let searchkey = ctx.request.body.searchkey
    let sql = `SELECT game.G_ID "id", game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc",game.G_URL "gameurl" FROM game where G_NAME like '%${searchkey}%'`
    ctx.body = await new Promise((resolve, reject) => {
        return db.query(sql, [],(data) => {
            data.map(val => {
                val.url = 'http://localhost:5000/' + val.url
            })
            resolve(data)
        })
    })
})

module.exports = search
